package com.example.flutter_plugin_1

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

/** FlutterPlugin_1Plugin */
public class FlutterPlugin_1Plugin: FlutterPlugin, MethodCallHandler, ActivityAware {

  //新的 注册插件方式
  private lateinit var activityContext: Context

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    this.activityContext = flutterPluginBinding.applicationContext
    val channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_plugin_1")
    channel.setMethodCallHandler(this);
  }

  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val channel = MethodChannel(registrar.messenger(), "flutter_plugin_1")
      channel.setMethodCallHandler(FlutterPlugin_1Plugin())
    }
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android - ${android.os.Build.VERSION.RELEASE}")
    } else if (call.method == "uninstall") {
      //解析参数
      val packageName: String? = call.argument("flutter")
      var checkApplication = checkApplication(packageName)
      if (checkApplication) {
        Uninstall(packageName)
      }
      //返回给flutter的参数
      result.success(checkApplication)
    }
    else {
      result.notImplemented()
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {

  }

  private fun Uninstall(packageName: String?) {
    val packageURI: Uri = Uri.parse("package:$packageName")
    val intent = Intent(Intent.ACTION_DELETE)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.setData(packageURI)
    activityContext!!.startActivity(intent)
  }


  fun checkApplication(packageName: String?): Boolean {
    return if (packageName == null || "".equals(packageName)) {
      false
    } else try {

      var packageManager = activityContext!!.packageManager
      var applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
      return applicationInfo != null
    } catch (e: PackageManager.NameNotFoundException) {
      false
    }
  }

  override fun onDetachedFromActivity() {

  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {

  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activityContext = binding.activity
  }

  override fun onDetachedFromActivityForConfigChanges() {

  }
}
